package com.huiquan.answer.dao;

import com.huiquan.analysis.dao.BaseDao;
import com.huiquan.answer.domain.Answer;
import com.ibatis.sqlmap.client.SqlMapClient;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lichao email:lichao@witspring.com
 * @Description:
 * @Date 2018/2/8 09:52
 * @since Ver V
 */
@Repository
public class AnswerDao extends BaseDao {
    @Resource(name = "sqlMapClient")
    private SqlMapClient sqlMapClient;

    public static final String space = "ANSWER.";

    public Answer selectAnswerById(long id) {
        return (Answer) this.object(space + "SELECT_ANSWER_BY_ID", id, sqlMapClient);
    }

    /**
     * 得到问答服务数据版本数量
     *
     * @return
     */
    public int selectAnswerVersionCount() {
        Integer count = (Integer) this.object(space + "SELECT_ANSWER_VERSION_COUNT", sqlMapClient);
        if (count == null) {
            return 0;
        } else {
            return count;
        }
    }

    /**
     * 得到问答服务数据版本列表
     *
     * @param param
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<String> selectAnswerVersionList(Map<String, Object> param) {
        return (List<String>) this.list(space + "SELECT_ANSWER_VERSION_LIST", param, sqlMapClient);
    }

    /**
     * 得到问答服务数据数量
     *
     * @return
     */
    public int selectAnswerCount(Map<String, Object> param) {
        Integer count = (Integer) this.object(space + "SELECT_ANSWER_COUNT", param, sqlMapClient);
        if (count == null) {
            return 0;
        } else {
            return count;
        }
    }

    /**
     * 得到问答服务数据列表
     *
     * @param param
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Answer> selectAnswerList(Map<String, Object> param) {
        return (List<Answer>) this.list(space + "SELECT_ANSWER_LIST", param, sqlMapClient);
    }

    public void updateFlag(Map<String, Object> param) {
        this.update(space + "UPDATE_FLAG", param, sqlMapClient);
    }

    /**
     * 判断问答库数据是否存在
     *
     * @param answer
     * @return
     */
    public boolean isExistWithAnswerRepo(Answer answer) {
        int count = (int) this.object(space + "SELECT_ANSWER_REPO_COUNT", answer, sqlMapClient);
        return count > 0;
    }

    /**
     * 插入一条数据到问答库
     *
     * @param answer
     */
    public void insertAnswerRepo(Answer answer) {
        this.insert(space + "INSERT_ANSWER_REPO", answer, sqlMapClient);
    }

    /**
     * 更新一条数据到问答库
     *
     * @param answer
     */
    public void updateAnswerRepo(Answer answer) {
        this.update(space + "UPDATE_ANSWER_REPO", answer, sqlMapClient);
    }

    /**
     * 从临时表导入到bas_answer
     */
    public void insertDataFromTemp() {
        this.insert(space + "INSERT_DATA_FROM_TEMP", sqlMapClient);
    }

    /**
     * 根据bas_answer_repo数据更新bas_answer数据
     */
    public void updateDataByRepo() {
        this.update(space + "UPDATE_DATA_BY_REPO", sqlMapClient);
    }
}
